﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Module_Basic_Entities
{
    public class Layer2D : Layer
    {
        public Layer2D(List<List<Tile>> list, int mapWidth,
            int mapHeight, int tileWidth, int tileHeight, List<BLKSTR> blocks)
            : base(list, mapWidth,
                mapHeight, blocks)
        {
            this.TileWidth = tileWidth;
            this.TileHeight = tileHeight;
        }


        public int TileWidth
        {
            get;
            set;
        }

        public int TileHeight
        {
            get;
            set;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="objDestRect">Retângulo </param>
        /// <returns>Caso encontrado, é retornado o tile que</returns>
        public List<Tile> IntersectTiles(Rectangle objDestRect)
        {
            var tiles = from ts in this.Tiles
                        from t in ts
                        where Tile.Intersects(objDestRect, t)
                        select t;

            if (tiles.Count() == 0)
                return null;

            return tiles.ToList();
        }

        public AnimatedTile ContainsAnimatedTile(int line, int column)
        {
            if (this.AnimatedTiles == null) return null;
            var tiles = from ts in this.AnimatedTiles
                        where AnimatedTile.Contains(line,column, ts)
                        select ts;

            if (tiles.Count() == 0)
                return null;

            return tiles.First();
        }
        public Tile? ContainsTile(Vector2 pos)
        {
            var tiles = from ts in this.Tiles
                        from t in ts
                        where Tile.Contains(pos, t)
                        select t;

            if (tiles.Count() == 0)
                return null;
            
            return tiles.First();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="gameTime"></param>
        public void Draw(float gameTime, SpriteBatch spriteBatch, Vector2 worldPosition, Color color)
        {
            int row=0;
            
            foreach (var rowOftiles in this.Tiles)
            {
                int column = 0;
                foreach (var tile in rowOftiles)
                {
                    
                   AnimatedTile animTile = ContainsAnimatedTile(row, column);
                    if (animTile==null)
                    {
                        spriteBatch.Draw(this.TileStripTexture, worldPosition + tile.Position,
                        this.GetTileSourceRectangle(tile.Id), color);
                    }
                   
                    else
                    {
                        spriteBatch.Draw(this.TileStripTexture, worldPosition + tile.Position,
                         this.GetTileSourceRectangle(animTile.getCurrentFrameId()), color);
                        
                    }
                    column++;
                }
                 row++;
            }
        }

        public void MoveTiles(List<Tile> listTiles, int [] line, int [] column)
        {
            int count = 0;
            foreach (Tile tile in listTiles)
            {
                tiles[line[count]][line[count]] = tile;                       
            }

        }        
    }
}
